const bcrypt = require('bcryptjs')


exports.hash = (myPlainnextPassword) => {
    return new Promise((resolve, reject) => {
        bcrypt.hash(myPlainnextPassword, 10, function (err, hash) {
            if (err) {
                reject(err)
            } else {
                resolve(hash)
            }
        })
    })
}

// 随机id
exports.orderCode = () => {
    let orderCode = "";
    for (let i = 0; i < 6; i++) {
        orderCode += Math.floor(Math.random() * 10);
    }
    orderCode = new Date().getTime() + orderCode;
    return orderCode;
}

// 当前年月日
exports.getCurrentDay = () => {
    return new Date(+new Date() + 8 * 3600 * 1000).toJSON().substr(0, 19).split("T")[0];
}

// 当前时间 年月日时分秒
exports.getCurrentTime = () => {
    return new Date(+new Date() + 8 * 3600 * 1000).toJSON().substr(0, 19).replace("T", " ");
}

//将老的文件名拼上时间戳
exports.generate = () => {
    let d = new Date();
    return `${"" + d.getFullYear() + (d.getMonth() + 1) + d.getDate() + '_' + d.getHours() + d.getMinutes() + d.getSeconds()}`;
}

/**
 * 给文件名添加时间
 * @param {String} oldFilename 旧的文件名
 * @returns 
 */
exports.generateFilename = (oldFilename) => {
    //将老的文件名拼上时间戳
    let names = oldFilename.split(".");
    return `${names[0]}_${this.generate()}.${names[1]}`;
}